import type { LoginResult } from '@/types/userInfo'
import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义 Store
export const useUserInfoStore = defineStore(
  'userInfo',
  () => {
    // 会员信息
    const profile = ref<LoginResult>()
    const publishText = ref('')

    // 保存会员信息，登录时使用
    const setProfile = (val: LoginResult) => {
      profile.value = val
    }

    // 清理会员信息，退出时使用
    const clearProfile = () => {
      profile.value = undefined
    }

    const setPublishText = (val: string) => {
      publishText.value = val
    }

    const clearPublishText = () => {
      publishText.value = ''
    }

    // 记得 return
    return {
      publishText,
      profile,
      setProfile,
      clearProfile,
      setPublishText,
      clearPublishText,
    }
  },
  {
    // 网页端配置
    // persist: true,
    // 小程序端配置
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
// 确保有默认导出
export default useUserInfoStore
